package com.api.quartz.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.api.core.domain.vo.IpcVo;
import com.api.core.domain.vo.MonitoredVo;
import com.api.core.mapper.IpcMapper;
import com.api.quartz.domain.query.CRTabName;
import com.api.quartz.mapper.TaskMapper;
import com.api.quartz.service.ITaskService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class TaskServiceImpl implements ITaskService {
    @Autowired
    TaskMapper taskMapper;

    @Autowired
    IpcMapper ipcMapper;

    @Override
    public void refreshTables() {
        List<CRTabName> list = taskMapper.searchCRTabName();
        Set<String> ipcSet = new HashSet<>();
//        Map<String, JSONObject> elementCodeMap = new HashMap();
        List<MonitoredVo> monitoredVoList = new ArrayList<>();
        for (CRTabName item : list) {
            String tableName = item.getTableName();
            String key = "";
            JSONObject jo = new JSONObject();
            if (tableName.contains("resource")) {
                System.out.println("contains resource tableName: "+tableName);
                String[] items = tableName.split("_");
                if (items.length > 2) {
                    ipcSet.add(items[1]);
                }
            } else if (tableName.contains("tracing_serialization")
                    || tableName.contains("tracing_intergration")
                    || tableName.contains("tracing_evaluation")) {
//                System.out.println("contains other tableName: "+tableName);
                String monitoredCode = tableName.replace("_tracing_serialization","").replace("_tracing_intergration","").replace("_tracing_evaluation","").substring(4);
                MonitoredVo vo = new MonitoredVo();
                vo.setMonitoredCode(monitoredCode);
//                JSONObject defName = new JSONObject();
//                defName.put("zh-CN", "Default Name");
//                vo.setMonitoredNameLocales(defName);
//                vo.setGroupId("0");
                vo.setIpcCode(monitoredCode.split("_")[0]);
//                vo.setStrDatColSet("[2,3,5,6]");
                monitoredVoList.add(vo);
//                if (items.length > 5) {
//                    String ipcCode = items[1];
//                    ipcSet.add(ipcCode);
//                    Integer adSlot = Integer.parseInt(items[3]);
//                    key = ipcCode + "_" + items[3] + "_" + items[4];
//                    jo.put("ipcCode", ipcCode);
//                    jo.put("adSlot", adSlot);
//                    jo.put("elementCode", (adSlot - 1) * 64 + Integer.parseInt(items[4]));
//                    elementCodeMap.put(key, jo);
//                }

            } else {
                System.out.println("not contains tableName: "+tableName);
            }
//            000_002_resource
//            000_002_001_001_001_tracing_serialization
//            000_002_001_001_001_tracing_intergration
//            000_002_001_001_001_tracing_evaluation
        }
        if (ObjectUtils.isNotEmpty(ipcSet)) {
            insertOrUpIpcs(ipcSet);
        }
//        if (ObjectUtils.isNotEmpty(elementCodeMap)) {
//            insertOrUpElements(elementCodeMap);
//        }
        if(ObjectUtils.isNotEmpty(monitoredVoList)){
            insertOrUpMonitored(monitoredVoList);
        }
    }

    private void insertOrUpIpcs(Set<String> ipcSet) {
        JSONObject defIpcName = new JSONObject();
        defIpcName.put("zh-CN", "DefIpcName");
        for (String ipcCode : ipcSet) {
            IpcVo vo = new IpcVo();
            vo.setIpcNameLocales(defIpcName);
            vo.setIpcCode(ipcCode);
            try {
                taskMapper.insertOrUpIpc(vo);
            } catch (Exception e) {
            };
        }
    }

    private void insertOrUpMonitored(List<MonitoredVo> monitoredVoList){
        for(MonitoredVo vo: monitoredVoList){
            taskMapper.insertOrUpMonitored(vo);
        }
    }

    private void insertOrUpElements(Map<String, JSONObject> elementCodeMap) {
        JSONObject defElemName = new JSONObject();
        defElemName.put("zh-CN", "DefElementName");
        for (String key : elementCodeMap.keySet()) {
            try {
                JSONObject jo = elementCodeMap.get(key);
                Integer adSlot = jo.getInteger("adSlot");
                Integer intElementCode = jo.getInteger("elementCode");
                String ipcCode = jo.getString("ipcCode");
//                String elemCode = "00" + intElementCode;
//                elemCode = elemCode.substring(elemCode.length() - 3);
//                IpcVo ipcVo = new IpcVo();
//                ipcVo.setIpcCode(ipcCode);
//                List<IpcQuery> query = ipcMapper.list(ipcVo);
//                String ipcCode = query.get(0).getIpcCode();
//                if (ObjectUtils.isNotEmpty(taskMapper.searchElemQuery(ipcCode, elemCode))) {
//                    ElementVo vo = new ElementVo();
//                    vo.setElementNameLocales(defElemName);
//                    vo.setElementCode(elemCode);
//                    vo.setRobotId(0L);
//                    vo.setIpcId(ipcId);
//                    vo.setIpcSlot(adSlot);
//                    taskMapper.insertOrUpElement(vo);
//                }
            } catch (Exception e) {}
        }
    }

    ;
}
